package com.oracle.team.service;

import com.oracle.team.domain.*;
import static com.oracle.team.service.Data.*;

public class NameListService {
    private Employee[] employees;

    public NameListService() {
        //创建员工数组，长度为Employee数组长度
    	employees = new Employee[EMPLOYEES.length];

        //依次遍历，取出当前数组(一维数组)，再从数组里取出元素，创建对象（普通员工、程序员、设计师、架构师），放到员工数组里
    	for(int i=0; i<employees.length; i++) {
    		int type = Integer.parseInt(EMPLOYEES[i][0]);	//人员类别10-13
    		int id = Integer.parseInt(EMPLOYEES[i][1]);	//员工Id
    		String name = EMPLOYEES[i][2];
    		int age = Integer.parseInt(EMPLOYEES[i][3]);	
    		double salary = Double.parseDouble(EMPLOYEES[i][4]);
    		
    		double bouns=0;
    		int stock=0;
    		Equipment eqipment=null;
    		
    		if(type == EMPLOYEE) {	//普通员工
    			//创建员工对象
    			employees[i] = new Employee(id, name, age, salary);    			
    		}
    		else if(type == PROGRAMMER)	{	//程序员
    			//获取对应的装备
				/*
				 * int eqtype = Integer.parseInt(EQIPMENTS[i][0]); if(eqtype == PC) { //PC
				 * eqipment = new PC(EQIPMENTS[i][1], EQIPMENTS[i][2]); } else if(eqtype ==
				 * NOTEBOOK) { eqipment = new NoteBook(EQIPMENTS[i][1],
				 * Double.parseDouble(EQIPMENTS[i][2])); } else if(eqtype == PRINTER) { eqipment
				 * = new Printer(EQIPMENTS[i][1], EQIPMENTS[i][2]); }
				 */
    			
    			eqipment = getEquipment(i);
    			
    			employees[i] = new Programmer(id, name, age, salary, 0, Status.FREE, eqipment);    		
    		}
    		else if(type == DESIGNER) {
    			//获取对应的装备
    			eqipment = getEquipment(i);
    			
    			bouns = Double.parseDouble(EMPLOYEES[i][5]);
    			
    			employees[i] = new Designer(id, name, age, salary, 0, Status.FREE, eqipment, bouns);    			
    		}
    		else if(type == ARCHITECT) {
    			eqipment = getEquipment(i);
    			
    			bouns = Double.parseDouble(EMPLOYEES[i][5]);
    			stock = Integer.parseInt(EMPLOYEES[i][6]);
    			
    			employees[i] = new Architect(id, name, age, salary, 0, Status.FREE, eqipment, bouns, stock);    			
    		}
    	}
    }

    private Equipment getEquipment(int i) {
        //为程序员、设计师、架构师创建对应设备
    	//设备二维数组找到对应元素(一维数组)
    	//获取对应的装备
    	Equipment eqipment=null;
		int eqtype = Integer.parseInt(EQIPMENTS[i][0]);
		
		if(eqtype == PC) {	//PC
			eqipment = new PC(EQIPMENTS[i][1], EQIPMENTS[i][2]);    				
		}
		else if(eqtype == NOTEBOOK) {
			eqipment = new NoteBook(EQIPMENTS[i][1], Double.parseDouble(EQIPMENTS[i][2]));    				
		}
		else if(eqtype == PRINTER) {
			eqipment = new Printer(EQIPMENTS[i][1], EQIPMENTS[i][2]);			
		}
		
		return eqipment;
        
    }

    public Employee[] getAllEmployees() {
        //返回所有员工的数组
    	return employees;
    }

    public Employee getEmployee(int id) throws TeamException {
        //返回指定员工-循环便利，若当前员工号==id，表示查到了，返回此员工，退出循环
    	//若循环完毕仍然没有找到，则抛出异常
    	/*Employee emp = null;
    	
    	for(int i=0; i<employees.length; i++) {
    		Employee nowemp = employees[i];
    		if(nowemp.getId()==id) {
    			emp = nowemp;
    			break;
    		}    		
    	}
    	
    	//
    	if(emp!=null) {
    		return emp;
    	}
    	else {
    		throw new TeamException(id+"查无此人");
    	}*/
    	
    	//2优化代码
    	/*for(int i=0; i<employees.length; i++) {
    		Employee nowemp = employees[i];
    		if(nowemp.getId()==id) {
    			return nowemp;
    		}    		
    	}
    	throw new TeamException(id+"查无此人");*/
    	
    	//3继续优化
    	for(Employee emp : employees) {
    		if(emp.getId()==id) {
    			return emp;
    		}    		
    	}
      	throw new TeamException(id+"查无此人");    
      	
      	//4继续优化-判断id的范围，合理区间循环，否则直接抛出异常
      	
      	
    }
    
	/*
	 * public static void main(String[] args) { NameListService nameService = new
	 * NameListService(); int length = nameService.employees.length; Employee emp1 =
	 * nameService.employees[length-1]; System.out.println(emp1.getDetails()); }
	 */
}
